﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FrmHotel
{
    public partial class FrmShangPinGL : Form
    {
        public FrmShangPinGL()
        {
            InitializeComponent();
        }

        private void FrmShangPinGL_Load(object sender, EventArgs e)
        {
            ComBoxInfo();
            DataWioeVime1();
            DataWioeVime2();
        }
        public void ComBoxInfo()
        {
            string sql2 = "select * from  CommodityClass";
            DataTable dt2 = DBHelper.SelectForTable(sql2);
            DataRow dr2 = dt2.NewRow();
            dr2["ClassId"] = 0;
            dr2["ClassName"] = "全部";
            dt2.Rows.InsertAt(dr2, 0);
            this.comboBox1.DisplayMember = "ClassName";
            this.comboBox1.ValueMember = "ClassId";
            this.comboBox1.DataSource = dt2;
        }
        public void DataWioeVime1()
        {
            this.dataGridView1.AutoGenerateColumns = false;
            string sql = string.Format("select * from Commodity c,CommodityClass s where c.CyClass=s.ClassId ");
            string hao = this.textBox1.Text;
            if (!string.IsNullOrEmpty(hao))
            {
                sql += string.Format(" and c.CyName like '%{0}%' ", hao);
            }
            int id =Convert.ToInt32(this.comboBox1.SelectedValue);
            if (id!=0)
            {
                sql += string.Format(" and c.CyClass={0} ", id);
            }
            this.dataGridView1.DataSource = DBHelper.SelectForTable(sql);
        }
        public void DataWioeVime2()
        {
            this.dataGridView2.AutoGenerateColumns = false;
            string sql = string.Format("select * from CommodityClass where 1=1 ");
            string hao = this.textBox2.Text;
            if (!string.IsNullOrEmpty(hao))
            {
                sql += string.Format(" and   ClassName like '%{0}%' ", hao);
            }
            
            this.dataGridView2.DataSource = DBHelper.SelectForTable(sql);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataWioeVime1();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataWioeVime1();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            DataWioeVime2();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            FrmAddLieXing x = new FrmAddLieXing();
            x.ShowDialog();
            DataWioeVime2();
            DataWioeVime2();
            ComBoxInfo();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (this.dataGridView2.SelectedRows.Count <= 0)
            {
                MessageBox.Show("至少选择一条数据！");
                return;
            }
            int id = Convert.ToInt32(this.dataGridView2.SelectedRows[0].Cells[0].Value);
            FrmUpdateLieXing u = new FrmUpdateLieXing();
            u.id = id;
            u.ShowDialog();
            DataWioeVime2();
            ComBoxInfo();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.dataGridView2.SelectedRows.Count <= 0)
            {
                MessageBox.Show("至少选择一条数据！");
                return;
            }
             DialogResult dr = MessageBox.Show("真的要删除吗？","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
             if (dr == DialogResult.Yes)
             {
                 int id = Convert.ToInt32(this.dataGridView2.SelectedRows[0].Cells[0].Value);
                 string sql = string.Format("select count(1) from Commodity where CyClass={0}", id);
                 int result = Convert.ToInt32(DBHelper.SelectOne(sql));
                 if (result > 0)
                 {
                     MessageBox.Show("此类型下有商品不能删除！", "提示信息！");
                     return;
                 }
                 else
                 {
                     string sql1 = string.Format("delete from CommodityClass  where ClassId={0}", id);
                     int r = DBHelper.ExecuteUpdate(sql1);
                     if (r > 0)
                     {
                         MessageBox.Show("删除成功！", "提示信息！");
                         DataWioeVime2();
                         ComBoxInfo();
                         return;
                     }
                     else
                     {
                         MessageBox.Show("删除失败！", "提示信息！");
                         return;
                     }
                 }
             }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FrmAddShangPinInfo fp = new FrmAddShangPinInfo();
            fp.ShowDialog();
            DataWioeVime1();
            DataWioeVime2();
            ComBoxInfo();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count <= 0)
            {
                MessageBox.Show("至少选择一条数据！");
                return;
            }
            int id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value);
            FrmUpdateShangPinInfo fu = new FrmUpdateShangPinInfo();
            fu.id = id;
            fu.ShowDialog();
            DataWioeVime1();
            DataWioeVime2();
            ComBoxInfo();
        }

        private void button4_Click(object sender, EventArgs e)
        {

            if (this.dataGridView1.SelectedRows.Count <= 0)
            {
                MessageBox.Show("至少选择一条数据！");
                return;
            }

            int id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value);
            DialogResult dr = MessageBox.Show("真的要删除吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                string sql2 = string.Format("select count(1) from Consume where SsoName={0}", id);
                int shuling = Convert.ToInt32(DBHelper.SelectOne(sql2));
                if (shuling>0)
                {
                    MessageBox.Show("此商品销售中,不能删除！", "提示信息！");
                    return;
                }
                else
                {
                    string sql1 = string.Format("delete from Commodity  where CyId={0}", id);
                    int r = DBHelper.ExecuteUpdate(sql1);
                    if (r > 0)
                    {
                        MessageBox.Show("删除成功！", "提示信息！");
                        DataWioeVime2();
                        DataWioeVime1();
                        ComBoxInfo();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("删除失败！", "提示信息！");
                        return;
                    }
                }
            }
        }
    }
}
